package com.shanghaionstar.smart;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.iview.ISmartConnectingView;
import com.shanghaionstar.smart.presenter.SmartConnectingPresenter;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartConnectingFrg extends BaseFragment implements View.OnClickListener,ISmartConnectingView {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.connecting_iv)
    ImageView connectingIv;
    @FindViewById(R.id.connecting_tv)
    TextView connectingTv;
    @FindViewById(R.id.progress_tv)
    TextView mProgressTv;
    @FindViewById(R.id.bind_pl)
    ProgressLayout mBindPl;

    private BaseActivity mActivity;
    private SmartConnectingPresenter presenter;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_connect_progress;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        if(isFirstLoad()) {
            presenter = new SmartConnectingPresenter(mActivity.getScheduler());
            presenter.setView(this);
            Bundle bun = getArguments();
            presenter.getArguments(bun);
            presenter.bindDevice(connectingIv);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.resumeActiviteDevice();
    }

    private void initView() {
        mTitleTv.setText(R.string.smart_add_device);
    }


    @Override
    @OnClick({R.id.cancel_btn, R.id.back_ib})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_btn:
                BusinessUtil.sendReportByClickType(mActivity, FunctionNameFindId.Smarthome_setupnet_cancel.getStringValue());
                presenter.cancel();
                back();
                break;
            case R.id.back_ib:
                BusinessUtil.sendReportByClickType(mActivity, FunctionNameFindId.Smarthome_connecthomewifi_return.getStringValue());
                presenter.cancel();
                back();
                break;
        }
    }

    private void showFailedDia() {
        new AlertDialog.Builder(mActivity)
                .setMessage(getString(R.string.smart_bind_failed))
                .setPositiveButton(R.string.smart_try_again, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        back();
                    }
                })
                .setNegativeButton(R.string.smart_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        back();
                    }
                })
                .show();
    }




    @Override
    public void setLoading(boolean isLoading) {
        mBindPl.setVisibility(isLoading?View.VISIBLE:View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        showFailedDia();
    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return mActivity;
    }

    @Override
    public void connectError() {
        new ToastView(mActivity).showToast(getString(R.string.smart_bind_failed));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                back();
            }
        },500);
    }

    @Override
    public void showProgress(String progress) {
        mProgressTv.setText(progress);
    }
}
